package levelrewind.android.app;

import levelrewind.android.app.adapter.BattleHistoryListAdapter;
import levelrewind.android.app.model.RestDataHandler;
import levelrewind.android.app.vo.Battle;

import com.greystripe.sdk.GSMobileBannerAdView;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BattleHistoryListActivity extends Activity {
	
	private ListView battleListView;
	private Battle[] battles;
	private GSMobileBannerAdView adBanner;
	
	private TextView back;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_battle_history);
        
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        back = (TextView)findViewById(R.id.list_back);
        back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
        
        new getBattles().execute();
        
        //Add banner
    	adBanner = (GSMobileBannerAdView)this.findViewById(R.id.gsBanner);
    	adBanner.refresh();
    }
    
    private void getBattlesToList() {    	
    	battleListView = (ListView)findViewById(R.id.battle_history_list);
		
    	BattleHistoryListAdapter arrayAdapter = new BattleHistoryListAdapter(BattleHistoryListActivity.this);
		for(int i=0; i<battles.length;i++) {
			arrayAdapter.addItem(battles[i]);
		}
		battleListView.setAdapter(arrayAdapter);
		
		//Add banner, put list between line and banner
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    	params.addRule(RelativeLayout.BELOW, R.id.line);
    	params.addRule(RelativeLayout.ABOVE, R.id.gsBanner);
    	battleListView.setLayoutParams(params);
    }
    
    private class getBattles extends AsyncTask<Void, Void, Battle[]>
    {
    	protected void onPostExecute(Battle[] result)
    	{
			battles = result;
			if (battles != null) getBattlesToList();
		}
    	
		@Override
		protected Battle[] doInBackground(Void... params) {
			return RestDataHandler.getBattlesHistory(BattleHistoryListActivity.this);
		}
    	
    }
}
